package com.bristua.mall.user.biz;

import com.bristua.mall.common.utils.*;
import com.bristua.mall.user.dao.TUserCollectionMapper;
import com.bristua.mall.user.model.TUser;
import com.bristua.mall.user.model.TUserCollection;
import com.bristua.mall.user.model.condition.TUserCollectionExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/3/25.
 */
@Service
@Transactional
public class AddUserCollectionGoodsBiz {

    @Resource
    private TUserCollectionMapper tUserCollectionMapper;

    @Autowired
    private CacheUtil cacheUtil;

    @Autowired
    private DataCenterAPIUrl dataCenterAPIUrl;

    @Autowired
    private RestTemplate restTemplate;


    public void addUserCollectionGoods(String goodsId){
        TUser userSession = (TUser) cacheUtil.getUserSession();
        TUserCollectionExample tUserCollectionExample = new TUserCollectionExample();
        tUserCollectionExample.createCriteria().andGoodsIdEqualTo(goodsId).andUserIdEqualTo(userSession.getUserId());
        List<TUserCollection> tUserCollections = tUserCollectionMapper.selectByExample(tUserCollectionExample);
        if(tUserCollections==null || tUserCollections.size()==0){
            TUserCollection tUserCollection = new TUserCollection();
            tUserCollection.setUserCollectionId(SequenceUtil.nextId());
            tUserCollection.setCreateTime(DateUtil.getDateTime3());
            tUserCollection.setGoodsId(goodsId);
            Map map = queryGoodsById(goodsId);
            Object medias = map.get("medias");
            if(medias!=null && medias instanceof List){
                List imgUrls = (List) medias;
                if(imgUrls.size()>0){
                    Map o = (Map) imgUrls.get(0);
                    tUserCollection.setGoodsImgUrl(String.valueOf(o.get("url")));
                }

            }
            tUserCollection.setGoodsName(String.valueOf(map.get("name")));
            tUserCollection.setGoodsPrice((new BigDecimal(String.valueOf(map.get(("price")))).multiply(new BigDecimal("100"))).toString());
            tUserCollection.setUserId(userSession.getUserId());
            tUserCollectionMapper.insert(tUserCollection);
        }

    }



    public Map queryGoodsById(String goodsId){
        String url = dataCenterAPIUrl.getUrl()+ DataCenterAPIUrl.getGoodsDetailUrl;
        url = String.format(url, goodsId);
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
        String body = forEntity.getBody();
        Map map = JsonUtil.json2Map(body);
        String code = String.valueOf(Double.valueOf(String.valueOf(map.get("code"))).intValue());
        if("200".equals(code)){
            Map data = (Map)map.get("data");
            List list = (List) data.get("list");
            Object o = list.get(0);
            if(o!=null){
                Map goods = (Map)o;
                return goods;
            }
        }
        return null;
    }

}
